/**
 * 文件：CommentService.java
 * 作者：苏新阳
 * 完成度：完成
 * 最后修改日期：2023-7-4 01:22
 */
package com.gomokult.dao.impl;

import com.gomokult.dao.DisplayedGameDAO;
//import com.gomokult.dao.GameDAO;
//import com.gomokult.entity.Game;
import com.gomokult.dao.GameDAO;
import com.gomokult.dao.UserDAO;
import com.gomokult.entity.Game;
import com.gomokult.entity.GameDisplayInfo;
import com.gomokult.entity.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.gomokult.utility.JDBCUtil.con;
import static com.gomokult.utility.JDBCUtil.pstat;
//修改结束
public class DisplayedGameDAOImpl implements DisplayedGameDAO {
	private DisplayedGameDAOImpl() {}
	private static final DisplayedGameDAOImpl singleton = new DisplayedGameDAOImpl();
	public static DisplayedGameDAOImpl getSingleton() { return singleton; }

	private GameDAO gameDAO=GameDAOImpl.getSingleton();//注入gameDAO
	private UserDAO userDao=UserDAOImpl.getSingleton();//注入userDAO

	@Override
	public int getUserDisplayedGamesCount(int userID) {//正确
		try {
			int a = 0;//用来记录对局的数量
			String sql = "select * from displayedgames where user = ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1, userID);
			ResultSet rs = pstat.executeQuery();
			while(rs.next()){
				a+=1;//每查询到一条数据就自增一
			}
			return a;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<GameDisplayInfo> getUserDisplayedGamesFromPage(int start, int end, int userID) {//修改完
		try {
			List<GameDisplayInfo> games = new ArrayList<>();
			// 连接数据库,查询display_games表中userID对应的数据,返回start到end条记录
			String sql = "SELECT * FROM  displayedgames WHERE user = ? ORDER BY displayDate DESC limit ? offset ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1, userID);
			pstat.setInt(3, start);
			pstat.setInt(2, end);
			ResultSet rs = pstat.executeQuery();
			while (rs.next()) {
				System.out.println(1);
				int n=rs.getInt("game");//获取gameID
				int m=rs.getInt("user");//获取userID
				String description=rs.getString("description");
				Timestamp time=rs.getTimestamp("displayDate");
				Game game=new Game();
				User user=new User();
				game=gameDAO.getGame(n);
				user=userDao.selectUserbyID(m);
				GameDisplayInfo date=new GameDisplayInfo(game,user,description,time);
				games.add(date);
			}
			return games;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean isGameDisplayed(int userID, int gameID) {//正确
		try {
			String sql = "SELECT * FROM displayedgames WHERE user = ? AND game = ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1, userID);
			pstat.setInt(2, gameID);
			ResultSet rs = pstat.executeQuery();
			boolean result = rs.next();
			return result;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void addGameToDisplay(int userID, int gameID, String description) {//修改完
		try {Date date = new Date();
			java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
			String sql = "INSERT INTO displayedgames(user, game, description, displayDate) VALUES (?, ?, ?, ?)";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1, userID);
			pstat.setInt(2, gameID);
			pstat.setString(3, description);
			pstat.setTimestamp(4, timestamp);
			int rs = pstat.executeUpdate();
			System.out.println(rs);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void deletedisplayGame(int userID, int gameID) {//正确
		try {
			String sql = "DELETE FROM displayedgames WHERE user = ? AND game = ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1, userID);
			pstat.setInt(2, gameID);
			pstat.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void updateGame(GameDisplayInfo game) {

	}


	//
	public String getSettime(String value) {
		String time=null;
		//将Date类型的时间转换成指定格式的字符串
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		time = dateFormat.format(value);
		return time;
	}


}
